clear all

cd "$revdta"
set more off

set scheme s2mono

***************************Variable construction ***********************************************************************************************
capture program drop construct_vars_sp
program define construct_vars_sp
	xtset newid year
	sort newid
	merge m:1 newid using ".\intermediary\sp_temp_types"
	tab _merge
	drop if _merge!=3
	drop _merge
	sort newid year
	tab married, gen(M)
	gen lag1M1=l1.M1
	gen lag1M2=l1.M2

	/*pxy is the prob of moving from L=y to L=x */
	forvalues i=1(1)2{
	gen p`i'1=lag1M`i'==1 & M1==1					
	gen p`i'2=lag1M`i'==1 & M2==1
}

end
******************************************************************************************************************************************



**************************Predict values for the <=62 part (to use in simulations)
*tokenize lp healthspend healthdis prodwage
*foreach typetype in type_lp type_healthspend type_healthdis type_prodwage {
foreach var in "" {
tokenize $typename
foreach typetype in $type  {
*type_prodpost
cd "$revdta"
use "Full_Panel_2b.dta", clear

keep if sample_analytic==1 & educ <= 2

bysort newid: egen firstage = min(age)
bysort newid: egen firsthealth = max(DS * (age==firstage))


local suffix

*I lose most people before 1996 from child health, another 30% from restricting 
*to in-sample heads -- NO LOGNER DOING THIS, b/c I can impute divorce as a 
* reason for married to exit
*total: 2k households

*keep if marit==`relate'
expand 2 if sample_leavenext ==1 & married==1, gen(tag)
replace married = 0 if tag == 1
replace year = year+1 if tag == 1 & year <= 1996
replace year = year+2 if tag == 1 & year > 1996

construct_vars_sp
ren type_fix type_lp
ren `typetype' type_fix
levelsof type_fix, local(typevals)
keep if DS == 0
***Probits by type and controlling for a full set of age dummies. Done for the baseline <=62 sample.
foreach j of local typevals{
	forvalues i=1(1)2{
	forvalues k=1(1)2{
	quietly gen  p`i'`k'_t`j'=.
	forvalues a = 23(1)62{
	quietly sum p`i'`k' if lag1M`i'==1 & type_fix==`j'&age==`a'
	quietly replace p`i'`k'_t`j'=r(mean) if lag1M`i'==1 & type_fix==`j'&age==`a'
					}
					}
					}
					}
	foreach x of varlist p??_t?{
	gen c_`x' =`x'
	}
collapse (mean) p??_t? (count) c_p??_t?,by(age)
foreach x of varlist p??_t?{
replace `x' =. if c_`x'==0
}
drop c_p??_t?
keep if age>=23 & age<=62
sort age


foreach j of local typevals{
forvalues i=1(1)2{
egen rowtot`i'_t`j' = rowtotal(p`i'?_t`j'), missing
}
}

drop rowtot*

cd "$out"

save temp_2362,replace


**************************Predict values for the 62+ part (to use in simulations)
cd "$revdta"

use "Full_Panel_2b.dta", clear


keep if sample_select==1  & educ <= 2

bysort newid: egen firstage = min(age)
bysort newid: egen firsthealth = max(DS * (age==firstage))


local suffix


*keep if chd_maxhealth>=1 & chd_maxhealth<=2 
*I lose most people before 1996 from child health, another 30% from restricting 
*to in-sample heads
*total: 2k households

*keep if marit==`relate'
expand 2 if sample_leavenext ==1 & married==1, gen(tag)
replace married = 0 if tag == 1
replace year = year+1 if tag == 1 & year <= 1996
replace year = year+2 if tag == 1 & year > 1996



construct_vars_sp
g agesel=age>=23 & age<=82
ren type_fix type_lp
ren `typetype' type_fix

keep if DS == 0

cd "$out"

preserve

collapse (mean) married if age >=30 & age <= 50, by(type_fix)

export delimited using "marriagemeans`suffix'`1'", delimiter(",") replace

restore

tab type_fix, gen(types)

reg married types* if age>=30 & age <=50, nocons

***Probits linear in age done for the 23-82 sample, assuming uniform growth by type after age 62
quietly{
	forvalues i=1(1)2{
capture probit p`i'1 age if lag1M`i'==1 & agesel==1
	if _rc == 0 predict q`i'1,pr
	if _rc != 0 {
	sum p`i'1 if lag1M`i'==1 & agesel==1 
	gen q`i'1 = r(mean) 
	}
capture probit p`i'2 age if lag1M`i'==1 & agesel==1
	if _rc == 0 predict q`i'2,pr
	if _rc != 0 {
	sum p`i'2 if lag1M`i'==1 & agesel==1 
	gen q`i'2 = r(mean) 
	}


}
}					
collapse (mean) q?? ,by(age)
sort age

cd "$out"

merge age using temp_2362
drop _merge
keep if age>=23 & age<=82

****Now assumes transition probabilities for the 62+ grow using prob. linear in age (uniform growth by type) starting from flexible predictions by type at age 62
***Also deal with cases in which no flexible prediction at age 62 is available, so move back until flexible prediction is found
forvalues j=1(1)2	{
		forvalues k=1(1)2	{
			g g`j'`k'=q`j'`k'/q`j'`k'[_n-1]-1
							}
					}

forvalues j=1(1)2	{
		forvalues k=1(1)2	{
			foreach t of local typevals{
				replace p`j'`k'_t`t'=p`j'`k'_t`t'[_n-1]*(1+g`j'`k') if age>62 | p`j'`k'_t`t'==.
								}
							}
					}

***Finally, smooth (and interpolate to fill in empty age cells)
forvalues j=1(1)2	{
		forvalues k=1(1)2	{
			foreach t of local typevals{
				lowess p`j'`k'_t`t' age,gen(p`j'`k'_t`t'q) nog 
				ipolate p`j'`k'_t`t'q age,gen(p`j'`k'_t`t'p) e
				lab var p`j'`k'_t`t'p "Type `t'"
								}
							}
					}
*drop g* q* *q p11_t1-p33_t3
keep age p??_t?p p??_t?
rename p??_t? rawp??_t?
rename p*p p*
local reshapes
forvalues i=1(1)2{
forvalues j=1(1)2{
*collecting names for reshape
local reshapes `reshapes' p`i'`j'_t rawp`i'`j'_t
}
}
reshape long `reshapes', i(age) j(type)

forvalues i=1(1)2{
forvalues j=1(1)2{
*truncating negative interpolations at 0
replace p`i'`j'_t=0 if p`i'`j'_t < 0
replace rawp`i'`j'_t=0 if rawp`i'`j'_t < 0
}
}

rename p*_t p*
rename rawp*_t rawp*

reshape long p1 p2 rawp1 rawp2 , i(age type) j(to)
forvalues i=1(1)2{
bysort age type: egen p`i'tot = sum(p`i')
replace p`i'=p`i'/p`i'tot

bysort age type: egen rawp`i'tot = sum(rawp`i')
replace rawp`i'=rawp`i'/rawp`i'tot
}
drop p*tot rawp*tot

reshape long p rawp, i(age type to) j(from)

preserve
drop rawp
export delimited using "empiricalmarriageprobs_healthy`suffix'`1'", delimiter(",") replace
restore

keep if age <= 62

twoway (line p age if to == 2 & from == 1 & type==1, lwidth(medthick)) (line p age if to == 2 & from == 1 & type==2, lwidth(medthick) lpattern(shortdash)) (line p age if to == 2 & from == 1 & type==3, lwidth(medthick) lpattern(longdash)), legend(label(1 Low Type) label(2 Moderate Type) label(3 High Type)) name(Marriage, replace) ytitle(Marriage Probability, size(vlarge)) xtitle(Age, size(vlarge)) yscale(range(0 0.2)) ylabel(#6, labsize(vlarge)) xlabel(, labsize(vlarge)) plotregion(color(white)) graphregion(color(white)) 

twoway (line p age if to == 1 & from == 2 & type==1, lwidth(medthick)) (line p age if to == 1 & from == 2 & type==2, lwidth(medthick) lpattern(shortdash)) (line p age if to == 1 & from == 2& type==3, lwidth(medthick) lpattern(longdash)), legend(label(1 Low Type) label(2 Moderate Type) label(3 High Type)) name(Divorce, replace) ytitle(Separation Probability, size(vlarge)) xtitle(Age, size(vlarge)) yscale(range(0 0.2)) ylabel(#6, labsize(vlarge)) xlabel(, labsize(vlarge)) plotregion(color(white)) graphregion(color(white)) 

/*
local combtypes
foreach t of local typevals {
local combtypes `combtypes' Type`t'
twoway (connected p age if to==2 & from == 1) (connected p age if to==1 & from == 2) if type == `t',  legend(label(1 Marriage Risk) label(2 Divorce Risk))xtitle(Age, size(vlarge)) ytitle(Probability, size(vlarge)) name(Type`t', replace) title(Type `t')  legend(off) yscale(range(0 0.2)) ylabel(#6 , labsize(vlarge)) xlabel(, labsize(vlarge))   plotregion(color(white)) graphregion(color(white))
graph export "marriageshocks_type`t'_`1'.pdf", replace
}
*/
grc1leg Marriage Divorce, legendfrom(Marriage) plotregion(color(white)) graphregion(color(white))
graph export "marriageshocks_healthy`suffix'`1'.pdf", replace



cd "$revdta"

use "Full_Panel_2b.dta", clear

keep if sample_select==1  & educ <= 2

bysort newid: egen firstage = min(age)
bysort newid: egen firsthealth = max(DS * (age==firstage))


local suffix

*I lose most people before 1996 from child health, another 30% from restricting 
*to in-sample heads
*total: 2k households


expand 2 if sample_leavenext ==1 & married==1, gen(tag)
replace married = 0 if tag == 1
replace year = year+1 if tag == 1 & year <= 1996
replace year = year+2 if tag == 1 & year > 1996


merge m:1 newid using ".\intermediary\sp_temp_types"
keep if _merge ==3
drop _merge
*keep if marit==`relate'
keep if type_prodwage != .
tab married, gen(M)
g agesel=age>=23 & age<=82
ren type_fix type_lp
ren `typetype' type_fix

levelsof type_fix, local(typevals)

keep if DS == 0
					
collapse (mean) M? ,by(age type_fix)
sort age

cd "$out"
keep if age>=23 & age<=82
ren M? M?_
reshape wide M1_ M2_, i(age) j(type_fix)
****Now assumes transition probabilities for the 62+ grow using prob. linear in age (uniform growth by type) starting from flexible predictions by type at age 62
***Also deal with cases in which no flexible prediction at age 62 is available, so move back until flexible prediction is found


***Finally, smooth (and interpolate to fill in empty age cells)
forvalues j=1(1)2	{
			foreach t of local typevals{
				lowess M`j'_`t' age,gen(M`j'_`t'q) nog 
				ipolate M`j'_`t'q age,gen(M`j'_`t'p) e
				lab var M`j'_`t'p "Type `t'"
							}
					}
*drop g* q* *q p11_t1-p33_t3
keep age M?_?p M?_?
rename M?_? rawM?_?
rename M*p M*
local reshapes
forvalues i=1(1)2{
*collecting names for reshape
local reshapes `reshapes' M`i'_ rawM`i'_
}
reshape long `reshapes', i(age) j(type)
rename *_ *




preserve
drop rawM*
drop M1
rename M2 M
export delimited using "empiricalmarriagestocks_healthy`suffix'`1'", delimiter(",") replace
restore
macro shift
}

}
